/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gui.grupy_cwiczeniowe;

import db.models.GrupaCwiczeniowa;
import db.models.GrupaOcen;
import db.models.Student;
import db.models.Termin;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//org.gui.grupy_cwiczeniowe//GrupaCwiczeniowa//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = "GrupaCwiczeniowaTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "org.gui.grupy_cwiczeniowe.GrupaCwiczeniowaTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_GrupaCwiczeniowaAction",
        preferredID = "GrupaCwiczeniowaTopComponent"
)
@Messages({
    "CTL_GrupaCwiczeniowaAction=GrupaCwiczeniowa",
    "CTL_GrupaCwiczeniowaTopComponent=GrupaCwiczeniowa Window",
    "HINT_GrupaCwiczeniowaTopComponent=This is a GrupaCwiczeniowa window"
})
public final class GrupaCwiczeniowaTopComponent extends TopComponent {

    public GrupaCwiczeniowaTopComponent() {
        initComponents();
        setName(Bundle.CTL_GrupaCwiczeniowaTopComponent());
        setToolTipText(Bundle.HINT_GrupaCwiczeniowaTopComponent());

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        GroupName = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        SubjectName = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        studentsTable = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        termsTable = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        typ_oceniania = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        schemat_oceniania = new javax.swing.JLabel();

        GroupName.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(GroupName, org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.GroupName.text")); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.jLabel1.text")); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.jLabel2.text")); // NOI18N

        SubjectName.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(SubjectName, org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.SubjectName.text")); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.jLabel3.text")); // NOI18N

        studentsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Imie", "Nazwisko", "Email", "Indeks"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(studentsTable);
        if (studentsTable.getColumnModel().getColumnCount() > 0) {
            studentsTable.getColumnModel().getColumn(0).setResizable(false);
            studentsTable.getColumnModel().getColumn(0).setHeaderValue(org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.studentsTable.columnModel.title0")); // NOI18N
            studentsTable.getColumnModel().getColumn(1).setResizable(false);
            studentsTable.getColumnModel().getColumn(1).setHeaderValue(org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.studentsTable.columnModel.title1")); // NOI18N
            studentsTable.getColumnModel().getColumn(2).setResizable(false);
            studentsTable.getColumnModel().getColumn(2).setHeaderValue(org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.studentsTable.columnModel.title2")); // NOI18N
            studentsTable.getColumnModel().getColumn(3).setResizable(false);
            studentsTable.getColumnModel().getColumn(3).setHeaderValue(org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.studentsTable.columnModel.title3")); // NOI18N
        }

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.jLabel4.text")); // NOI18N

        termsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Dzień tygodnia", "Czas"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(termsTable);
        if (termsTable.getColumnModel().getColumnCount() > 0) {
            termsTable.getColumnModel().getColumn(0).setResizable(false);
            termsTable.getColumnModel().getColumn(0).setHeaderValue(org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.termsTable.columnModel.title0_2")); // NOI18N
            termsTable.getColumnModel().getColumn(1).setResizable(false);
            termsTable.getColumnModel().getColumn(1).setHeaderValue(org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.termsTable.columnModel.title1_2")); // NOI18N
        }

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.jLabel5.text")); // NOI18N

        typ_oceniania.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(typ_oceniania, org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.typ_oceniania.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel7, org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.jLabel7.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(schemat_oceniania, org.openide.util.NbBundle.getMessage(GrupaCwiczeniowaTopComponent.class, "GrupaCwiczeniowaTopComponent.schemat_oceniania.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(11, 11, 11)
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(GroupName))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(SubjectName))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel5)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(typ_oceniania, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(schemat_oceniania)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 139, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 343, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(GroupName)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(SubjectName))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(typ_oceniania))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(schemat_oceniania))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel GroupName;
    private javax.swing.JLabel SubjectName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel schemat_oceniania;
    private javax.swing.JTable studentsTable;
    private javax.swing.JTable termsTable;
    private javax.swing.JLabel typ_oceniania;
    // End of variables declaration//GEN-END:variables
    
    private GrupaCwiczeniowa grupa;
    
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
        if(this.GroupName.getText().equals("Nazwa grupy")) this.close();
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }
    
    public void setStudentsTable(GrupaCwiczeniowa grupa){
        
        List<Student> studenci = Student.getByGroup(grupa.getId());
        DefaultTableModel model = (DefaultTableModel) studentsTable.getModel();
        
        while(model.getRowCount() > 0) model.removeRow(0);
        
        for(Student s: studenci){
            model.addRow(new Object[]{s.getImie(),s.getNazwisko(),s.getEmail(),s.getIndeks()});
        }
        studentsTable.setModel(model);
    }
    
    public String getGroupName(){
        return this.GroupName.getText();
    }
    
    public void setTermsTable(GrupaCwiczeniowa grupa){
        List<Termin> terms = Termin.getByGroup(grupa.getId());
        DefaultTableModel model = (DefaultTableModel) termsTable.getModel();

        while(model.getRowCount() > 0) model.removeRow(0);
        
        for(Termin t: terms){
            model.addRow(new Object[]{t.getDzien_tygodnia().toString(),t.getGodzina_start()+" - "+t.getGodzina_stop()});
        }
        termsTable.setModel(model);
        
    }
    
    public void setGroup(GrupaCwiczeniowa grupa){
        this.grupa = grupa;
        this.GroupName.setText(grupa.getNazwa());
        this.SubjectName.setText(grupa.getPrzedmiot().getNazwa());
        this.typ_oceniania.setText(grupa.getPrzedmiot().getTypOceniania().toString());
        String schemat_oceniania = "<html>"+grupa.getPrzedmiot().getGrupaOcen().getNazwa()+"<br>";
        for(GrupaOcen g: GrupaOcen.getAllGrupaOcen(grupa.getPrzedmiot().getGrupaOcen().getId())) {
            schemat_oceniania += " &nbsp;&nbsp;&nbsp; - " + g.getNazwa() + "<br>";
        }
        schemat_oceniania += "</html>";
        this.schemat_oceniania.setText(schemat_oceniania);
        setDisplayName(grupa.getNazwa()+" - "+grupa.getPrzedmiot().getNazwa() + " - Panel Główny");
        setStudentsTable(grupa);
        setTermsTable(grupa);
    }
    
    public JTable getTable(){
        return termsTable;
    }
    
    public DefaultTableModel getTableModel(){
        return (DefaultTableModel) termsTable.getModel();
    }
    
    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    /**
     * @return the grupa
     */
    public GrupaCwiczeniowa getGrupa() {
        return grupa;
    }

    /**
     * @param grupa the grupa to set
     */
    public void setGrupa(GrupaCwiczeniowa grupa) {
        this.grupa = grupa;
    }
   
}
